﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using SalonPay.Entity;
using SalonPay.IBLL;
using System.Text;
using System.Text.RegularExpressions;
public partial class order : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void submit_ServerClick(object sender, EventArgs e)
    {
        //快钱充值流程：
        //0.验证账号
        //1.插入订单
        //2.提交快钱表单(order.aspx)进行支付
        //3.快钱调用(reveive99bill.aspx)接口，更新订单状态（支付成功或支付失败）
        //4.订单支付成功，调用游戏服务器接口充值。


        //验证账号是否存在
        IUser userOpt = RemotingHelper.UserService();
        string user_msg = userOpt.CheckUserName(this.txtUserName.Text);
        if (user_msg.Split(new char[1] { '|' }, StringSplitOptions.None)[0] != "true")
        {
            //该账号不存在
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入正确的账号');document.getElementById('txtUserName').select();</script>");
            return;
        }
       

        //生成订单
        IOrder orderOpt = RemotingHelper.OrderService();

        tb_Order orderInfo = new tb_Order();

        int orderAmount;//处理金额
        
        Regex re = new Regex(@"^[1-9]\d+$");

        if (re.IsMatch(this.txtOrderAmount.Text))
        {
            orderInfo.OAmount = Convert.ToInt32(this.txtOrderAmount.Text);  //沙龙金额单位元
            if (orderInfo.OAmount <= 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入正确的充值金额');document.getElementById('txtOrderAmount').select();</script>");
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入正确的充值金额');document.getElementById('txtOrderAmount').select();</script>");
            return;
        }

        orderInfo.OEmail = "";
        orderInfo.OLanguage = Convert.ToInt32(ConfigurationManager.AppSettings["99Bill_language"]);
        orderInfo.OMemoText ="";
        orderInfo.OMoneyType = "RMB";
        orderInfo.OPayDate = DateTime.Now;
        orderInfo.OPayGateWayType = 1;//快钱1
        orderInfo.OPayIP = Request.UserHostAddress;
        orderInfo.OPayType = Convert.ToInt32(ConfigurationManager.AppSettings["99Bill_payType"]);
        orderInfo.OPhone = "";
        orderInfo.OrderID = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        orderInfo.OTrueName = "";
        orderInfo.OUserName = this.txtUserName.Text;
        //orderInfo.OPayType = Convert.ToInt32(Request.Form["bankId"]);//銀行id
        
        bool result = orderOpt.AddOrder(orderInfo);
        if (result)
        {
            //生成快钱表单
            Create99BillForm(orderInfo);

            #region 记录订单请求日志

            tb_PaySendLog log = new tb_PaySendLog();
            log.LBgUrl = ConfigurationManager.AppSettings["99Bill_bgUrl"];
            log.LMerchantAccId = ConfigurationManager.AppSettings["99Bill_merchantAcctId"];
            log.LOrderAmount = orderInfo.OAmount;
            log.LOrderId = orderInfo.OrderID;
            log.LOrderTime = orderInfo.OPayDate;
            log.LPayerName = orderInfo.OUserName;
            log.LPayGateWayType = Convert.ToByte(orderInfo.OPayGateWayType);
            log.LPayType = orderInfo.OPayType.ToString();
            
            orderOpt.AddSendLog(log);

            #endregion
        }
        else
        {
            //生成订单失败
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('系统繁忙,请稍候再试');</script>");

        }
    }

    public void Create99BillForm(tb_Order orderInfo)
    {
        //生成加密签名串
        string signMsgVal = "";
        //signMsgVal = common.appendParam(signMsgVal, "inputCharset", ConfigurationManager.AppSettings["99Bill_inputCharset"]);
        signMsgVal = common.appendParam(signMsgVal, "bgUrl", ConfigurationManager.AppSettings["99Bill_bgUrl"]);
        signMsgVal = common.appendParam(signMsgVal, "version", ConfigurationManager.AppSettings["99Bill_version"]);
        signMsgVal = common.appendParam(signMsgVal, "language", ConfigurationManager.AppSettings["99Bill_language"]);
        signMsgVal = common.appendParam(signMsgVal, "signType", ConfigurationManager.AppSettings["99Bill_signType"]);
        signMsgVal = common.appendParam(signMsgVal, "merchantAcctId", ConfigurationManager.AppSettings["99Bill_merchantAcctId"]);
        signMsgVal = common.appendParam(signMsgVal, "payerName", orderInfo.OUserName);
        signMsgVal = common.appendParam(signMsgVal, "payerContactType", "1");
        signMsgVal = common.appendParam(signMsgVal, "payerContact", orderInfo.OEmail);
        signMsgVal = common.appendParam(signMsgVal, "orderId", orderInfo.OrderID);
        signMsgVal = common.appendParam(signMsgVal, "orderAmount", Convert.ToString(orderInfo.OAmount*100));//快钱金额单位元
        signMsgVal = common.appendParam(signMsgVal, "orderTime", orderInfo.OPayDate.ToString("yyyyMMddhhmmss"));
        signMsgVal = common.appendParam(signMsgVal, "payType", ConfigurationManager.AppSettings["99Bill_payType"]);
        signMsgVal = common.appendParam(signMsgVal, "bankId", orderInfo.OPayType.ToString());
        //signMsgVal = common.appendParam(signMsgVal, "redoFlag",ConfigurationManager.AppSettings["99Bill_redoFlag"]);
        signMsgVal = common.appendParam(signMsgVal, "key", ConfigurationManager.AppSettings["99Bill_key"]);

        string signMD5 = common.GetMD5(signMsgVal, "utf-8").ToUpper();


        StringBuilder sb = new StringBuilder();
        sb.Append("<html><body>");
        sb.Append("<form name='99bill' method=\"post\" action='" + ConfigurationManager.AppSettings["99Bill_actionUrl"] + "'>");
        //sb.Append("<input type=\"hidden\" id=\"inputCharset\" value='"+ConfigurationManager.AppSettings["99Bill_inputCharset"] +"'/>");
        sb.Append("<input type=\"hidden\" name=\"bgUrl\" value='" + ConfigurationManager.AppSettings["99Bill_bgUrl"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"version\" value='" + ConfigurationManager.AppSettings["99Bill_version"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"language\" value='" + ConfigurationManager.AppSettings["99Bill_language"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"signType\" value='" + ConfigurationManager.AppSettings["99Bill_signType"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"signMsg\" value='" + signMD5 + "'/>");
        sb.Append("<input type=\"hidden\" name=\"merchantAcctId\" value='" + ConfigurationManager.AppSettings["99Bill_merchantAcctId"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"payerName\" value='" + orderInfo.OUserName + "'/>");
        sb.Append("<input type=\"hidden\" name=\"payerContactType\" value=\"1\"/>");
        sb.Append("<input type=\"hidden\" name=\"payerContact\" value='" + orderInfo.OEmail + "'/>");
        sb.Append("<input type=\"hidden\" name=\"orderId\" value='" + orderInfo.OrderID + "'/>");
        sb.Append("<input type=\"hidden\" name=\"orderAmount\" value='" + Convert.ToString(orderInfo.OAmount*100) + "'/>");
        sb.Append("<input type=\"hidden\" name=\"orderTime\" value='" + orderInfo.OPayDate.ToString("yyyyMMddhhmmss") + "'/>");
        sb.Append("<input type=\"hidden\" name=\"payType\" value='" + ConfigurationManager.AppSettings["99Bill_payType"] + "'/>");
        sb.Append("<input type=\"hidden\" name=\"bankId\" value='" + orderInfo.OPayType.ToString() + "'/>");

        //sb.Append("<input type=\"hidden\" name=\"redoFlag\" value='" + ConfigurationManager.AppSettings["99Bill_redoFlag"] + "'/>");

        sb.Append("</form>");
        sb.Append("<script>document.forms[0].submit();</script>");
        sb.Append("</body></html>");


        Response.Write(sb.ToString());

    }
}
